package com.yzyz.service.impl;

import java.util.List;

import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import com.yzyz.bean.region.RegionBean;
import com.yzyz.dao.RegionDao;
import com.yzyz.service.RegionService;
import com.yzyz.service.ScenicService;
import com.yzyz.utils.error.Error;

/**
 * 
 * @author David Sun
 * @Date   2010-3-28
 *
 */
@Transactional
public class RegionServiceImpl implements RegionService {
	private RegionDao regionDao;
	
	private ScenicService scenicService;
	
	
	public void setScenicService(ScenicService scenicService) {
		this.scenicService = scenicService;
	}


	public void setRegionDao(RegionDao regionDao) {
		this.regionDao = regionDao;
	}

	@Override
	public List<RegionBean> listAllRegions() {
		return this.regionDao.getAll();
	}

	@Override
	public int add(Object object) {
		this.regionDao.save(object);
		return Error.SUCCESS;
	}
	@Transactional(propagation = Propagation.REQUIRED,rollbackFor=Exception.class)
	@Override
	public int delete(int id) {
		RegionBean region = (RegionBean)this.regionDao.get(id);
		region.setDeleted(true);
		if(this.regionDao.delete(id) != 1){
			return Error.OBJECT_NOT_EXIST;
		}
		return Error.SUCCESS;
		
	}
	@Transactional(propagation = Propagation.REQUIRED,rollbackFor=Exception.class)
	@Override
	public Object find(int id) {
		return this.regionDao.get(id);
	}
	@Transactional(propagation = Propagation.REQUIRED,rollbackFor=Exception.class)
	@Override
	public int update(Object object) {
		this.regionDao.update(object);
		return Error.SUCCESS;
		
	}

	@Override
	public List<RegionBean> listAllRegionWithoutDisabled() {
		List<RegionBean> regions =  this.regionDao.getAllWithoutDisabled();
		for(RegionBean region: regions) {
			region.setScenics(this.scenicService.listAllByRegion(region.getId(),0,1000));
		}
		return regions;
	}

	@Override
	public List<RegionBean> listRegions(int start, int limit) {
		if(start < 0) {
			start = 0;
		}
		if(limit < 30) {
			limit = 0;
		}
		return this.regionDao.getRegions(start, limit);
	}

	@Override
	public int getCount() {
		return this.regionDao.getCount(-1);
	}
	@Transactional(propagation = Propagation.REQUIRED,rollbackFor=Exception.class)
	@Override
	public int disable(int regionId) {
		this.regionDao.updateAvailable(regionId, false);

		return Error.SUCCESS;
	}
	@Transactional(propagation = Propagation.REQUIRED,rollbackFor=Exception.class)
	@Override
	public int enable(int regionId) {
		this.regionDao.updateAvailable(regionId, true);
		return Error.SUCCESS;
	}

	@Override
	public List<RegionBean> init() {
		List<RegionBean> regions = this.regionDao.getAllWithoutDisabled();
		if(regions != null){
			for(RegionBean region: regions){
				region.setScenics(this.scenicService.init(region.getId()));
			}
		}
		return regions;
	}


	@Override
	public List<?> paginate(int start, int limit) {
		// TODO Auto-generated method stub
		return null;
	}

}
